local gears          = require("gears")
local awful          = require("awful")
local hotkeys_popup  = require("awful.hotkeys_popup")
local calendar_popup = require("awful.widget.calendar_popup")
local menubar        = require("menubar")

require("keybinds.dev")

globalkeys = gears.table.join(
    -- View hotkeys
    awful.key({ modkey, }, "s", function() hotkeys_popup.show_help(nil, awful.screen.focused()) end,
        {description = "View hotkeys", group = "global"}
    ),
    -- View previous desktop
    awful.key({ modkey, }, "Left",   awful.tag.viewprev,
        {description = "View previous desktop", group = "desktop"}
    ),
    -- View next desktop
    awful.key({ modkey, }, "Right",  awful.tag.viewnext,
        {description = "View next desktop",     group = "desktop"}
    ),
    -- Go back desktop
    awful.key({ modkey, }, "Escape", awful.tag.history.restore,
       {description = "Go back desktop",        group = "desktop"}
    ),
    
    -- Hide bar
    awful.key({modkey,            }, "b",
        function()
            myscreen = awful.screen.focused()
            myscreen.mywibox.visible = not myscreen.mywibox.visible
        end,
        {description = "Toggle hide wibar", group = "desktop"}
    ),
    
    -- Focus next window by index
    awful.key({ modkey,           }, "j",
        function ()
            awful.client.focus.byidx( 1)
        end,
        {description = "Focus next window by index", group = "window"}
    ),
    -- Focus previous window by index
    awful.key({ modkey,           }, "k",
        function ()
            awful.client.focus.byidx(-1)
        end,
        {description = "Focus previous window by index", group = "window"}
    ),

    -- App Switch
    awful.key({ modkey, "Shift" }, "Tab", function () awful.spawn("rofi -show window") end),

    -- Resize window, as I wish
    awful.key({ modkey, "Mod1" }, "h", function () awful.tag.incmwfact(-0.01)   end),
    awful.key({ modkey, "Mod1" }, "l", function () awful.tag.incmwfact( 0.01)   end),
    awful.key({ modkey, "Mod1" }, "j", function () awful.client.incwfact( 0.05) end),
    awful.key({ modkey, "Mod1" }, "k", function () awful.client.incwfact(-0.05) end),

    
    -- Sound volume and mic manipulation
    -- Decrease sound volume
    awful.key({}, "#122",          function () update_volume_widget(volume_widget, "-") end,
        {description = "Decrease sound volume", group = "media"}
    ),
    -- Increase sound volume
    awful.key({}, "#123",          function () update_volume_widget(volume_widget, "+") end,
        {description = "Increase sound volume", group = "media"}
    ),
    -- Toggle mute sound
    awful.key({}, "XF86AudioMute", function () update_volume_widget(volume_widget, "m") end,
        {description = "Toggle mute sound", group = "media"}
    ),
    
    -- Media control
    awful.key({         }, "XF86AudioPlay", function() awful.util.spawn("playerctl play-pause",    false) end,
        {description = "Play/pause player", group = "media"}
    ),
    awful.key({ "Shift" }, "XF86AudioPlay", function() awful.util.spawn("playerctl -a pause",      false) end,
        {description = "Pause all players", group = "media"}
    ),
    awful.key({ "Ctrl"  }, "XF86AudioPlay", function() awful.util.spawn("playerctl -a play-pause", false) end,
        {description = "Reverse all players", group = "media"}
    ),
    awful.key({         }, "XF86AudioNext", function() awful.util.spawn("playerctl next",          false) end,
        {description = "Play next", group = "media"}
    ),
    awful.key({         }, "XF86AudioPrev", function() awful.util.spawn("playerctl previous",      false) end,
        {description = "Play previous", group = "media"}
    ),
    awful.key({ modkey, "Mod1"}, "#72", function () awful.spawn(shPath.."change_your_mind.sh") end,
        {description = "Stream audio to discord", group = "media"}
    ),
    
    -- Brightness and Monitor control
    -- Decrease brightness level
    awful.key({}, "#232", function () update_brightness("-") end,
        {description = "Decrease brightness level", group = "screen"}
    ),
    -- Increase brightness level
    awful.key({}, "#233", function () update_brightness("+") end,
        {description = "Increase brightness level", group = "screen"}
    ),
    
    -- Some input devices
    -- Toggle touchpad
    awful.key({ modkey,         }, "#72", function () update_dev("touchpad") end,
        {description = "Toggle touchpad", group = "input devs"}
    ),
    -- Toggle Wacom tablet 
    awful.key({ modkey,         }, "#73", function () update_dev("wacom") end,
        {description = "Toggle wacom", group = "input devs"}
    ),
    -- Toggle touchpad notify
    awful.key({ modkey, "Shift" }, "#72", function () toggle_dev_notify("touchpad") end,
       {description = "Toggle touchpad notify", group = "input devs"}
    ),
    
    -- Apps
    -- alt terminal
    awful.key({ modkey, "Mod1" }, "Return", function () awful.spawn("cool-retro-term") end,
        {description = "Open alternative terminal(cool retro term)", group = "apps"}
    ),
    -- Make a screenshot
    awful.key({}, "Print", function () awful.spawn(shPath.."flameshot.sh s") end,
        {description = "Make screenshot of screen under cursor", group = "screenshot"}
    ),
    awful.key({ "Ctrl" }, "Print", function () awful.spawn(shPath.."flameshot.sh f") end,
        {description = "Make full screenshot", group = "screenshot"}
    ),
    awful.key({ modkey }, "Print", function () awful.spawn(shPath.."flameshot.sh a") end,
       {description = "Make area screenshot", group = "screenshot"}),
    -- pcmanfm
    awful.key({ modkey, }, "e",   function () awful.spawn("pcmanfm")          end,
        {description = "Open a pcmanfm file manager", group = "apps"}
    ),
    awful.key({ modkey, "Mod1", }, "e", function() awful.spawn("pcmanfm-qt") end,
        {description = "Qt version of pcmanfm", group = "apps"}
    ),
    -- Chromium
    awful.key({ modkey, }, "#67", function () awful.spawn("chromium-bin")  end,
        {description = "Open a Chromium web-browser", group = "apps"}
    ),
    -- Pavucontrol
    awful.key({ modkey, }, "#68", function () awful.spawn("pavucontrol")    end,
        {description = "Open a pavucontrol", group = "apps"}
    ),
    -- Telegram
    awful.key({ modkey, }, "#69", function () awful.spawn("telegram-desktop") end,
        {description = "Open a Telegram", group = "apps"}
    ),
    -- Gimp
    awful.key({ modkey, }, "#70", function () awful.spawn("gimp")             end,
        {description = "Open a GIMP an image manipulation and paint program", group = "apps"}
    ),
    -- Firefox
    awful.key({ modkey, }, "#71", function () awful.spawn("firefox-bin")          end,
        {description = "Open a Firefox web-browser", group = "apps"}
    ),

    -- Clementine
    awful.key({ modkey, "Mod1" }, "#67", function () awful.spawn("clementine") end,
        {description = "Open a Clementine music player", group = "apps"}
    ),
    -- Blender
    awful.key({ modkey, "Mod1" }, "#68", function () awful.spawn("blender-2.93") end,
        {description = "Open Blender", group = "apps"}
    ),
    -- Discord
    awful.key({ modkey, "Mod1" }, "#69", function () awful.spawn("discord") end,
        {description = "Open Discord", group = "apps"}
    ),
    -- Krita
    awful.key({ modkey, "Mod1" }, "#70", function () awful.spawn("krita") end,
        {description = "Krita", group = "apps"}
    ),
    -- LibreWolf
    awful.key({ modkey, "Mod1" }, "#71", function () awful.spawn("librewolf-bin") end,
        {description = "Open a LibreWolf web-browser", group = "apps"}
    ),

    
    -- Layout manipulation
    -- Swap window with next client by index
    awful.key({ modkey, "Shift"   }, "j", function () awful.client.swap.byidx(  1)    end,
        {description = "Swap window with next client by index", group = "layout"}
    ),
    -- Swap window with previous client by index
    awful.key({ modkey, "Shift"   }, "k", function () awful.client.swap.byidx( -1)    end,
        {description = "Swap window with previous client by index", group = "layout"}
    ),
    -- Focus the next screen
    awful.key({ modkey, "Control" }, "j", function () awful.screen.focus_relative( 1) end,
        {description = "Focus the next screen", group = "layout"}
    ),
    -- Focus the previous screen
    awful.key({ modkey, "Control" }, "k", function () awful.screen.focus_relative(-1) end,
        {description = "Focues the previous screen", group = "layout"}
    ),
    -- Jump to urgent client
    awful.key({ modkey,           }, "u", awful.client.urgent.jumpto,
        {description = "Jump to urgent client", group = "layout"}
    ),
    -- Go back window
    awful.key({ modkey,           }, "Tab",
        function ()
            awful.client.focus.history.previous()
            if client.focus then
                client.focus:raise()
            end
        end,
        {description = "Go back window", group = "layout"}
    ),
    
    -- Standard program
    -- Open a terminal
    awful.key({ modkey,           }, "Return", function () awful.spawn(terminal) end,
        {description = "Open a terminal", group = "std program"}
    ),
    -- Open a Emacs test editor
    awful.key({ modkey, "Shift"   }, "Return", function () awful.spawn("emacs")  end,
       {description = "Open an Emacs text editor", group = "std program"}
    ),
    -- Restart wm
    awful.key({ modkey, "Shift" }, "r", function () awful.spawn(shPath.."reset.sh") end,
        {description = "Restart WM", group = "std program"}
    ),
    awful.key({ modkey, "Control" }, "r",      awesome.restart,
        {description = "Restart WM", group = "std program"}
    ),
    -- Exit wm
    awful.key({ modkey, "Shift"   }, "e",      awesome.quit,
        {description = "Exit WM", group = "std program"}
    ),
    -- Lock screen
    awful.key({ "Control", "Mod1" }, "l",      function () awful.spawn(shPath.."lock.sh") end ,
        {description = "Lock screen", group = "std program"}
    ),
    					      
    -- Increase master width factor
    awful.key({ modkey,           }, "l",      function () awful.tag.incmwfact( 0.01)          end,
        {description = "Increase master width factor", group = "master"}
    ),
    -- Decrease master width factor
    awful.key({ modkey,           }, "h",      function () awful.tag.incmwfact(-0.01)          end,
        {description = "Decrease master width factor", group = "master"}
    ),
    -- Increase the number of master clients
    awful.key({ modkey, "Shift"   }, "h",      function () awful.tag.incnmaster( 1, nil, true) end,
        {description = "Increase the number of master clients", group = "master"}
    ),
    -- Decrease the number of master clients
    awful.key({ modkey, "Shift"   }, "l",      function () awful.tag.incnmaster(-1, nil, true) end,
        {description = "Decrease the number of master clients", group = "master"}
    ),
    -- Increase the number of columns
    awful.key({ modkey, "Control" }, "h",      function () awful.tag.incncol( 1, nil, true)    end,
        {description = "Increase the number of columns", group = "master"}
    ),
    -- Decrease the number of columns
    awful.key({ modkey, "Control" }, "l",      function () awful.tag.incncol(-1, nil, true)    end,
        {description = "Decrease the number of columns", group = "master"}
    ),
    -- Select next suit
    awful.key({ modkey,           }, "space",  function () awful.layout.inc( 1)                end,
        {description = "Select next suit", group = "layout"}
    ),
    -- Select prev suit
    awful.key({ modkey, "Shift"   }, "space",  function () awful.layout.inc(-1)                end,
        {description = "Select previous suit", group = "layout"}
    ),
    
    -- Restore minimized
    awful.key({ modkey, "Control" }, "n",
        function ()
            local c = awful.client.restore()
            -- Focus restored client
            if c then
              c:emit_signal(
                  "request::activate", "key.unminimize", {raise = true}
              )
            end
        end,
        {description = "Restire minimized", group = "window"}
    ),
    
    -- Prompt
    -- sh prompt
    awful.key({ modkey },            "r", function () awful.screen.focused().mypromptbox:run() end,
        {description = "sh prompt", group = "desktop"}
    ),
    -- lua prompt
    awful.key({ modkey }, "x",
        function ()
            awful.prompt.run {
                prompt       = "Run Lua code: ",
                textbox      = awful.screen.focused().mypromptbox.widget,
                exe_callback = awful.util.eval,
                history_path = awful.util.get_cache_dir() .. "/history_eval"
            }
        end,
        {description = "lua prompt", group = "desktop"}
    ),

    -- Menubar
    -- Show the menubar
    awful.key({ modkey }, "p", function() menubar.show() end,
        {description = "Show a menubar", group = "desktop"}
    )
)
